package com.meixiang.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.CartGoodsEntity;
import com.meixiang.entity.shopping.CartGroupEntity;
import com.meixiang.util.GlideHelper;

import java.util.List;

/**
 * 订单上显示商品的View
 *
 * @author LZRUI
 */
public class OrderGoodsView extends LinearLayout {

    private Context mContext;

    public OrderGoodsView(Context context) {
        this(context, null);
    }

    public OrderGoodsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("NewApi")
    public OrderGoodsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        setOrientation(VERTICAL);
    }

    /**
     * 获取布局上的控件
     * inflate 里面的parent参数需要设置成this，否则无法用用布局上的配置
     */
    private View getView(String goodsName, String storePrice, String goodsNum, String specInfo, String goodsImage) {
        View goodsLayout = LayoutInflater.from(mContext).inflate(R.layout.layout_order_goods, this, false);
        ImageView ivGoodsLogo = (ImageView) goodsLayout.findViewById(R.id.iv_goods_logo);
        TextView tvGoodsName = (TextView) goodsLayout.findViewById(R.id.tv_goods_name);
        TextView tvGoodsTag = (TextView) goodsLayout.findViewById(R.id.tv_goods_tag);
        TextView tvGoodsNewPrice = (TextView) goodsLayout.findViewById(R.id.tv_goods_price);
        TextView tvBuyNum = (TextView) goodsLayout.findViewById(R.id.tv_buy_num);
        // TODO 设置图片
        GlideHelper.showImage(mContext, goodsImage, R.drawable.image_default_gray_bg, ivGoodsLogo);
        tvGoodsName.setText(goodsName);
        tvGoodsNewPrice.setText(String.format(mContext.getString(R.string.price_str), storePrice));
        tvGoodsTag.setText(specInfo);
        tvBuyNum.setText(String.format(mContext.getString(R.string.goods_buy_num), goodsNum));
        return goodsLayout;
    }

    /**
     * 设置数据
     * 这边一定要给lineView设置 LinearLayout.LayoutParams
     *
     * @param goodsList
     */
    public void setGoodsList(List<CartGoodsEntity> goodsList) {
        if (goodsList == null) return;
        // 清除子View
        removeAllViews();
        int len = goodsList.size();
        for (int i = 0; i < len; i++) {
            CartGoodsEntity goods = goodsList.get(i);
            if (goods.isGroup()) {
                // 套装商品
                List<CartGroupEntity> groupList = goods.getGoodsGroupList();
                int groupLen = groupList.size();
                for (int j = 0; j < groupLen; j++) {
                    CartGroupEntity group = groupList.get(j);
                    setListItem(group.getGoodsName(), group.getGoodsStorePrice(), goods.getGoodsNum(), group.getSpecInfoString(mContext), group.getGoodsImage(), i == len - 1);
                }
            } else {
                setListItem(goods.getGoodsName(), goods.getGoodsStorePrice(), goods.getGoodsNum(), goods.getSpecInfoString(mContext), goods.getGoodsImage(), i == len - 1);
            }

        }
    }

    private void setListItem(String goodsName, String storePrice, String goodsNum, String specInfo, String goodsImage, boolean isLast) {
        View commentView = getView(goodsName, storePrice, goodsNum, specInfo, goodsImage);
        addView(commentView);
        LineView lineView = new LineView(mContext);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
        lineView.setLayoutParams(lp);
        lineView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.shopping_car_item_bg));
        if (!isLast) {
            lineView.setMarginLeft(15);
        }
        addView(lineView);
    }

}
